#!/bin/sh

# Author: Zhang Huangbin (michaelbibby <at> gmail.com)
# $Date$
# $Revision$

ECHO_INFO()
{
    echo -e "\n${OUTPUT_FLAG} $@"
}

backup_file()
{
    for conf_file in $@; do
        if [ -f ${conf_file} ]; then
            echo -e "\n${OUTPUT_FLAG} Backup configuration file: ${conf_file} -> ${conf_file}.${DATE}.\n"
            cp ${conf_file} ${conf_file}.${DATE}
        else
            :
        fi
    done
}

install_pkg()
{
    echo "${OUTPUT_FLAG} Installing package(s): $@"
    yum -d 1 -y install $@
}

extract_pkg()
{
    if [ X"$2" = X"" ]; then
        DST='.'
    else
        DST="$2"
    fi

    if echo $1 | grep '.tar.gz$' 2>/dev/null ; then
        ECHO_INFO "Extract package $1 to ${DST}"
        tar zxf $1 -C $DST
    elif echo $1 | grep '.tar.bz2$' 2>/dev/null ; then
        # Install bzip2 first.
        which bzip2 >/dev/null 2>&1

        if [ X"$?" != X"0" ]; then
            install_pkg bzip2
        else
            :
        fi

        ECHO_INFO "Extract package $1 to ${DST}"
        tar xjf $1 -C $DST
    else
        ECHO_INFO "Unknown archive format."
    fi
}
# ------------------------------------------------
# ---- Check necessery files/dirs.
# ------------------------------------------------
check_env()
{
    echo -n "Check configuration file: ${CONFIG_FILE}..."
    if [ -f ${CONFIG_FILE} ]; then
        echo -e "\tFound."
        echo -n "Use it for mail server setting? [Y|n]"
        read ANSWER

        case  $ANSWER in
            N|n )
                echo "Skip configuration file: ${CONFIG_FILE}."
                . ${CONFIG_VIA_DIALOG}
                ;;
            Y|y|* )
                echo "Use configuration file: ${CONFIG_FILE} for mail server setting."
                . ${CONFIG_FILE}
                ;;
        esac
    else
        echo -e "\tNone."
        . ${CONFIG_VIA_DIALOG}
    fi
}
